package org.eproject.core.hibernate.dao;

import java.io.Serializable;
import java.util.List;

import org.eproject.core.hibernate.pojo.SUsers;
import org.eproject.core.hibernate.session.HibernateSessionFactory;
import org.eproject.core.well.delegate.PageInfo;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

public class UserDaoImp implements IDao{

	public void delete(Class clazz, Serializable id) {
		
	}

	public Integer deleteAll(Class clazz) {
		// TODO Auto-generated method stub
		return null;
	}

	public Integer deleteByQuery(String queryString, Object[] parameters) {
		// TODO Auto-generated method stub
		return null;
	}

	public List find(String queryString) {
		Session s = (Session)HibernateSessionFactory.getSession();
		return null;
	}

	public List find(String queryString, Object[] parameters) {
		Session session = HibernateSessionFactory.getSession();
		SQLQuery sq = session.createSQLQuery(queryString);
		sq.addEntity("u",SUsers.class);
		for(int i=0;i<parameters.length;i++)
			sq.setString(i,parameters[i].toString());
		return sq.list();
	}

	public List findAll(Class clazz) {
		// TODO Auto-generated method stub
		return null;
	}

	public List findByNamedQuery(String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

	public List findByNamedQuery(String queryString, Object parameter) {
		// TODO Auto-generated method stub
		return null;
	}

	public List findByNamedQuery(String queryString, Object[] parameters) {
		// TODO Auto-generated method stub
		return null;
	}

	public List findPageByQuery(String queryString, Object[] parameters, PageInfo pageInfo) {
		Session session = HibernateSessionFactory.getSession();
		Query sq = session.createQuery(queryString);
		int pagesize = pageInfo.getPagesize();
		int pagecrre = pageInfo.getPagecrre();
		int first = (pagesize*pagecrre)-pagesize;
		sq.setFirstResult(first);
		sq.setMaxResults(pagesize);
		return sq.list();
	}

	public Object get(Class clazz, Serializable id) {
		Session s = (Session) HibernateSessionFactory.getSession();
		Object o = s.get(clazz,id);
		return o;
	}

	public Object get(String queryString, Object[] parameters) {
		
		return null;
	}

	public Object load(Class clazz, Serializable id) {
		Session s = (Session) HibernateSessionFactory.getSession();
		Object o = s.load(clazz,id);
		return o;
	}

	public void save(Object object) {
		Session s = (Session)HibernateSessionFactory.getSession();
		s.save(object);
		s.beginTransaction().commit();
	}

	public void update(Object object) {
		// TODO Auto-generated method stub
		
	}

	public void updateByQuery(String queryString, Object[] parameters) {
		// TODO Auto-generated method stub
		
	}

	public Object getRecordCount() {
		Session session = HibernateSessionFactory.getSession();
		Query q = session.createQuery("select count(*) from SUsers u");
		return q.uniqueResult();
	}
}